/**
 * @作者 zw
 * @微信公号 搬砖暗夜码农
 * @开源项目 $ https://gitee.com/zhouwei5208888/spring-boot-distributed
 * @我的博客 $ http://itzhouwei.com
 * @创建时间 2023/3/27 9:13 下午
 */
package com.itzhouwei.commerce.iplist.core.aspect;


import com.itzhouwei.commerce.iplist.core.annotation.DoIpList;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.BeanUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @description: 切面操作
 * @author：zw，微信公共号：yingshengzw
 * @date: 2021/8/28
 * @Copyright：公众号：搬砖暗夜码农 | 博客：https://itzhouwei.com - 沉淀、学习、分享、成长，让自己和他人都能有所收获！
 */
@SuppressWarnings("all")
@Slf4j
@Component
@Aspect
public class IpAspectConfig {

//    @Resource
//    private IpListProperties ipListProperties;

    @Value("${zhouwei.zws:127.0.0.2}")
    private String zws;


    /****
     * <h1>增强操作 </h1>
     * @param joinPoint 链接点
     * @param ipList 切点
     */
    @Around(value = "@annotation(ipList)")
    public Object doAroundCache(ProceedingJoinPoint joinPoint, DoIpList ipList) throws Throwable {
        //获取方法
        Method method = getMethod(joinPoint);
        //获取注解
        DoIpList annotation = method.getAnnotation(DoIpList.class);
        //获取字段值
        String filed = getFiled(annotation.key(), joinPoint.getArgs());
        log.info("字段值为    = " + filed);
        if (null == filed || "".equals(filed)) {
            //执行原有的方法
            return joinPoint.proceed();
        }
        //判断是否包含白名单

        String[] split = zws.split(",");

        for (String obj : split) {
            if (filed.equals(obj)) {
                //放行 执行原来的方法
                return joinPoint.proceed();
            }
        }
        //拦截
        return Error2Message(annotation, method);


    }

    //构建错误信息
    private Object Error2Message(DoIpList annotation, Method method) throws Exception {
        Class<?> returnType = method.getReturnType();
        String message = annotation.message();
        if ("".equals(message)) {
            return returnType.newInstance();
        }
       throw new RuntimeException(message);

    }

    //获取属性值
    private String getFiled(String key, Object[] args) throws InvocationTargetException, IllegalAccessException, NoSuchMethodException {
        String filed = "";
        for (Object arg : args) {
            try {
                if (null == filed || "".equalsIgnoreCase(filed)) {
                    filed = BeanUtils.getProperty(arg, filed);
                } else {
                    break;
                }
            } catch (Exception e) {
                if (args.length == 1) {
                    return args[0].toString();
                }
            }
        }
        return filed;

    }

    //获取方法
    private Method getMethod(ProceedingJoinPoint joinPoint) throws NoSuchMethodException {
        Signature signature = joinPoint.getSignature();

        MethodSignature methodSignature = (MethodSignature) signature;
        return joinPoint.getTarget().getClass().getMethod(methodSignature.getName()
                , methodSignature.getParameterTypes());

    }
}
